package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.kafka.common.serialization.ExtendedSerializer;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        if (channelDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        channelDto.checkParam();
        // 设置查询的参数
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (channelDto.getName() != null) {
            lambdaQueryWrapper.like(AdChannel::getName, channelDto.getName());
        }
        // 设置分页的参数
        Page pageInfo = new Page(channelDto.getPage(), channelDto.getSize());
        // 查询处理的数据
        IPage<AdChannel> page = page(pageInfo, lambdaQueryWrapper);
        ResponseResult result = new PageResponseResult(channelDto.getPage(), (int) page.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }


    @Override
    public ResponseResult add(AdChannel adChannel) {
        // 参数检查
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult editChannel(AdChannel adChannel) {
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 先查询这个频道
//        update(Wrappers.<AdChannel>update().notExists("select name "))
        AdChannel one = getOne(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
        if (one==null){
            updateById(adChannel);
            return ResponseResult.okResult(adChannel);
        }else {
            return ResponseResult.errorResult(0,"频道名已存在");
        }


    }

    @Override
    public ResponseResult deleteChannel(Integer adChannelId) {
        if (adChannelId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        AdChannel byId = getById(adChannelId);

        if (byId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (byId.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "该频道已被开启");
        }
        removeById(adChannelId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);


    }
}
